#include<cstring>
#include<iostream>
#include<cstdio>
#define R register
using namespace std;
int ans,n,siz[1005];
char s[1005][35],lx[1005][35];
bool checkin(int x)
{
	R int ds=0,ms=0,last=0;
	for (int i=0;i<siz[x];++i)
	{
		if (s[x][i]=='.')
		{
			if (last==i) return false;
			ds++;
			R int res=0;
			for (int j=last;j<=i-1;++j)
			{
				if (j-1==last&&res==0) return false;
				res=res*10+s[x][j]-'0';
				if (res>255) return false;
			}
			last=i+1;
		}
		if (s[x][i]==':')
		{
			if (last==i||siz[x]-1==i) return false;
			ms++;
			R int res=0;
			for (int j=last;j<=i-1;++j)
			{
				if (j-1==last&&res==0) return false;
				res=res*10+s[x][j]-'0';
				if (res>255) return false;
			}
			res=0;
			for (int j=i+1;j<siz[x];++j)
			{
				if (s[x][j]=='.'||s[x][j]==':') return false;
				if (j-1==i+1&&res==0) return false;
				res=res*10+s[x][j]-'0';
				if (res>65535) return false;
			}
		}
	}
	if (ds==3&&ms==1) return true;
	else return false;
}
void findin(int x)
{
	ans=0;
	for (R int j=1;j<x;++j)
		if (lx[j][0]=='S'&&siz[j]==siz[x])
		{
			ans=j;
			for (R int k=0;k<siz[x];++k)
				if (s[x][k]!=s[j][k])
				{
					ans=0;break;
				}
			if (ans) return;
		}
	return;
}
int main()
{
	freopen("network.in","r",stdin);freopen("network.out","w",stdout);
	scanf("%d",&n);
	for (R int i=1;i<=n;++i) scanf("%s",lx[i]),scanf("%s",s[i]),siz[i]=strlen(s[i]);
	for (R int i=1;i<=n;++i)
	{
		if (lx[i][0]=='S')
		{
			if (checkin(i)==false)
			{
				printf("ERR\n");
			}
			else
			{
				findin(i);
				if (ans) printf("FAIL\n");
				else printf("OK\n");
			}
		}
		if (lx[i][0]=='C')
		{
			if (checkin(i)==false)
			{
				printf("ERR\n");
			}
			else
			{
				findin(i);
				if (ans) printf("%d\n",ans);
				else printf("FAIL\n");
			}
		}
	}
	return 0;
}
